<template>
    <q-select
      dense
      outlined
      options-densetext
      v-model="cascade"
      class="col"
      :options="cascadeOptions"
      emit-value
      map-options
    >
      <cascade :dataList="cascadeList" :form=form :cascadeList="cascadeList" :level="1" @selCascade="selCascade"/>
    </q-select>
</template>

<script>
import Cascade from './cascade';

export default {
  props: {
    cascadeList: {
      type: Array,
    },
    rules: {
      type: Array,
      required: false,
      default: () => [],
    },
    formData: {
      type: Object,
      required: false,
    },
    formKey: {
      type: String,
      required: false,
    },
    form: {
      type: Object,
      required: false,
    }
  },
  components: { Cascade },
  data() {
    return {
      cascade: '',
      cascadeOptions: [],
      nameList: [],
    };
  },
  created() {

  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
    },
    selCascade(cascade) {
      this.cascadeOptions = [{ label: cascade.name, value: cascade.id }];
      this.cascade = cascade.id;
      this.thisList();
    },
    thisList() {
      this.cascadeList.forEach((item,index) => {
        if (item.children) {
          console.log('zhe   ');
          // 执行getId函数并将带有children的数组项以及你知道的末级id传过去
          this.forFn(item,this.cascade);
        } else {
          this.nameList.push(item.name);
        }
      });
    },
    forFn(arr,id) {
      if (arr.id === id) {
        this.nameList.push(arr.name);
      } else {
        arr.children.forEach((sitem,sindex) => {
          if (sitem.id === id) {
            this.nameList.push(sitem.id);
            this.forFn(arr,sitem.parentId);
          } else if (sitem.children) {
            this.forFn(sitem,id);
          }
        });
      }
    },

  },
};
</script>

<style scoped lang="stylus"></style>
